package com.luxsoft.sw3.kio.ui.form;

import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

import com.jgoodies.binding.adapter.BasicComponentFactory;
import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.uifextras.panel.HeaderPanel;
import com.luxsoft.siipap.model.core.Cliente;
import com.luxsoft.siipap.pos.ui.selectores.SelectorDeClientes;
import com.luxsoft.siipap.swing.actions.DispatchingAction;
import com.luxsoft.siipap.swing.binding.Binder;
import com.luxsoft.siipap.swing.form2.AbstractForm;
import com.luxsoft.siipap.swing.form2.DefaultFormModel;
import com.luxsoft.siipap.swing.form2.IFormModel;
import com.luxsoft.siipap.swing.utils.FormatUtils;
import com.luxsoft.siipap.swing.utils.ResourcesUtils;
import com.luxsoft.sw3.gasoc.model.Socio;
import com.luxsoft.sw3.services.Services;
import com.luxsoft.sw3.ui.catalogos.ClienteController;



/**
 * Forma para el mantenimiento de Clientes desde el punto de venta
 * 
 * @author Ruben Cancino
 *
 */
public class SociosForm extends AbstractForm{
	
	private JTabbedPane tabPanel;
	

	public SociosForm(IFormModel model) {
		super(model);
		setTitle("Mantenimiento de Socio");
	}

	@Override
	protected JComponent buildHeader() {
		if(model.getValue("id")!=null)
			return new HeaderPanel("Registro de socio nuevo ","");
		return null;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected JComponent buildFormPanel() {
		tabPanel=new JTabbedPane();
		tabPanel.addTab("Registro", buildGeneralForm());
		tabPanel.addTab("Servicios", buildProductosPanel());
		return tabPanel;
	}
	
	private JComponent buildGeneralForm(){
		final FormLayout layout=new FormLayout(
				"p,2dlu,p:g(.5), 2dlu," +
				"p,2dlu,p:g(.5)"
				,""
				);
		final DefaultFormBuilder builder=new DefaultFormBuilder(layout);
		builder.setDefaultDialogBorder();
		builder.appendSeparator("Datos generales");
		if(model.isReadOnly()){
			builder.append("Id",getControl("id"),true);
		}
		builder.append("Cliente",buildClienteControl(),5);
		builder.append("Apellido P",getControl("apellidoP"));
		builder.append("Apellido M",getControl("apellidoM"));
		builder.append("Nombre(s)",getControl("nombres"),5);
		builder.append("Folio",getControl("socio"),true);
		builder.append("Email",getControl("email"),5);		
		builder.append("Telefono",getControl("telefono1"));
		builder.append("Telefono 2",getControl("telefono2"));
		builder.append("Vigente",getControl("vigente"));
		builder.append("Preferente",getControl("preferente"));
		return builder.getPanel();
	}
	
	private JComponent buildProductosPanel(){
		ServiciosPorSocioPanel panel=new ServiciosPorSocioPanel((Socio)model.getBaseBean());
		panel.setEnabled(!model.isReadOnly());
		return panel;
	}

	
	@Override
	protected JComponent createCustomComponent(String property) {		
		if("nombre".equals(property) || "nombres".equals(property) || "apellidoP".equals(property) || "apellidoM".equals(property)){
			JTextField tf=Binder.createMayusculasTextField(model.getComponentModel(property),true);
			return tf;
		}
		return null;
	}
	
	private JLabel clienteLabel;
	
	private JComponent buildClienteControl(){
		FormLayout layout=new FormLayout("70dlu:g,2dlu,p,2dlu,p","");
		final DefaultFormBuilder builder=new DefaultFormBuilder(layout);
		JButton bt1=new JButton(getLookupAction());
		bt1.setFocusable(false);
		JButton bt2=new JButton(getInsertCliente());
		bt2.setFocusable(false);
		clienteLabel=BasicComponentFactory.createLabel(model.getModel("cliente"),FormatUtils.getToStringFormat());
		builder.append(clienteLabel,bt1,bt2);
		return builder.getPanel();
	}
	private Action lookupAction;
	private Action insertCliente;
	public Action getLookupAction(){
		if(lookupAction==null){
			lookupAction=new DispatchingAction(this,"seleccionarCliente");
			lookupAction.putValue(Action.SMALL_ICON, ResourcesUtils.getIconFromResource("images/misc2/page_find.png"));
			lookupAction.putValue(Action.NAME, "F2");
		}
		return lookupAction;
	}
	
	public Action getInsertCliente(){
		if(insertCliente==null){
			insertCliente=new DispatchingAction(this,"insertarClienteNuevo");
			insertCliente.putValue(Action.SMALL_ICON, ResourcesUtils.getIconFromResource("images2/user_add.png"));
			insertCliente.putValue(Action.NAME, "F7");			
		}
		return insertCliente;
	}
	
	public void insertarClienteNuevo(){
		Cliente c=ClienteController.getInstance().registrar();
		if(c!=null)
			model.setValue("cliente", c);
	}
	public void seleccionarCliente(){
		Cliente c=SelectorDeClientes.seleccionar(this);
			if(c!=null)
				model.setValue("cliente", c);
		
	}
	
	/**
	 * Prueba local en el EDT
	 * 
	 * @param args
	 * 
	 */
	public static void main(String[] args) {
		javax.swing.SwingUtilities.invokeLater(new Runnable() {

			public void run() {
				com.luxsoft.siipap.swing.utils.SWExtUIManager.setup();
				Socio socio=new Socio();
				DefaultFormModel model=new DefaultFormModel(socio);
				SociosForm form=new SociosForm(model);
				form.open();
				if(!form.hasBeenCanceled()){
					Services.getInstance().getHibernateTemplate().merge(model.getBaseBean());
				}
				System.exit(0);
			}

		});
	}
}
